package com.ytjj.qmyx.mall.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.qmyx.mall.model.request.ReturnAddExpressRequest;
import com.ytjj.qmyx.mall.model.request.ReturnCommitRequest;
import com.ytjj.qmyx.mall.service.ReturnService;
import com.ytjj.qmyx.mall.utils.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 退货控制器
 *
 * 
 *  *
 **/
@RestController
@RequestMapping("/return/goods")
@Slf4j
public class ReturnGoodsController extends BaseController {

    @Resource
    private ReturnService returnService;

    @GetMapping("/create")
    public CommonResult create(@RequestParam Integer orderInfoId) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(returnService.create(currentUser.getId(), orderInfoId));
    }

    /**
     * 主要是返回订单商品信息
     * @param orderInfoId
     * @return
     */
    @GetMapping("/appCreate")
    public CommonResult appCreate(@RequestParam Integer orderInfoId) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(returnService.appCreate(currentUser.getId(), orderInfoId));
    }
    /**
     * app提交退货申请-改成一步完成
     * @param request
     * @return
     */
    @PostMapping("/appCommit")
    public CommonResult appCommit(@RequestBody ReturnCommitRequest request) {
        UsersResponse currentUser = getCurrentUser();
        request.setUserId(currentUser.getId());
        return returnService.appComminReturn(request);
    }


    @GetMapping("/addAppCommitRedis")
    public CommonResult addAppCommitRedis() {
        return returnService.addAppCommitRedis();
    }


    /**
     * 提交退货申请
     */
    @PostMapping("/commit")
    public CommonResult commit(@Validated @RequestBody ReturnCommitRequest request) {
        UsersResponse currentUser = getCurrentUser();
        request.setUserId(currentUser.getId());
        returnService.commitReturn(request);
        return CommonResult.success();
    }

    /**
     * 提交快递单号
     */
    @PostMapping("/addExpress")
    public CommonResult addExpress(@Validated @RequestBody ReturnAddExpressRequest request) {
        UsersResponse currentUser = getCurrentUser();
        request.setUserId(currentUser.getId());
        returnService.addExpress(request);
        return CommonResult.success();
    }

    /**
     *
     * 根据快递单号获取物流公司
     */
    @GetMapping("/queryExpressCompany")
    public CommonResult queryExpressCompany(@RequestParam String num) {
        return CommonResult.success(returnService.queryExpressCompany(num));
    }

    /**
     * 获取退货状态
     */
    @GetMapping("/getReturnStatus")
    public CommonResult getReturnStatus (@RequestParam String serviceNo) {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(returnService.getReturnStatus(serviceNo,currentUser.getId()));
    }

    /**
     * 获取退货列表
     */
    @GetMapping("/list")
    public CommonResult getReturnList() {
        UsersResponse currentUser = getCurrentUser();
        return CommonResult.success(returnService.getReturnList(currentUser.getId()));
    }

    /**
     * 撤销
     */
    @GetMapping("revoke")
    public CommonResult revoke(@RequestParam String serviceNo) {
        returnService.revoke(serviceNo,getCurrentUser().getToken());
        return CommonResult.success();
    }

    /**
     * 获取退货快递信息
     */
    @GetMapping("getExpress")
    public CommonResult getExpress (@RequestParam String serviceNo) {
        return CommonResult.success(returnService.getExpress(serviceNo));
    }


    /**
     * 获取售后列表
     */
    @GetMapping("/lists")
    public ApiResult getReturnGoodsList(Integer pageNum,Integer pageSize,Integer type) {
        UsersResponse currentUser = getCurrentUser();
        return ApiResult.success(returnService.selectReturnGoods(pageNum,pageSize,type,currentUser.getToken()));
    }

    /**
     * 获取退货进度
     * @param returnGoodsId 退货表id
     * @return
     */
    @GetMapping("/getReturnGoodsProgress")
    public CommonResult getReturnGoodsProgress(@RequestParam Integer returnGoodsId){
        return returnService.getReturnGoodsProgress(returnGoodsId);
    }

    /**
     * 获取快递公司信息
     */
    @GetMapping("/getExpressCompany")
    public CommonResult getExpressCompany(){
        return returnService.getExpressCompany();
    }
}
